package com.oocl.euc.daoTest;

import static org.junit.Assert.*;

import java.io.Console;
import java.util.List;

import org.bson.types.ObjectId;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.alibaba.fastjson.JSON;
import com.oocl.euc.dao.MerchantDao;
import com.oocl.euc.dao.impl.MerchantDaoImpl;
import com.oocl.euc.entity.Merchant;
import com.oocl.euc.pagination.BaseConditionQuery;
import com.oocl.euc.pagination.SearchFilter;
import com.oocl.euc.util.JSONUtil;

public class MerchantDaoImplTest {
	private static ApplicationContext ac;
	private static MerchantDao mdi;
	@BeforeClass
	public static void init(){
		ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		mdi = (MerchantDao)ac.getBean("merchantDao");
	}
	@Test
	public void testAddMerchant() {
		Merchant m=new Merchant();
		m.setAccount("base");
		m.setPassword("lll");
		Assert.assertTrue(mdi.addMerchant(m)==1);
	}

	@Test
	public void testDeleteMerchant() {
		Assert.assertTrue(mdi.deleteMerchant("57ff2472b8828c231c20bd84")==1);
	}

	@Test
	public void testFindMerchantByMid() {
		String account=mdi.findMerchantByMid("57ff2780b8828c1ddcc67367").getAccount();
		System.out.println(account);
	}

	@Test
	public void testFindAllMerchants() {
		List<Merchant> merchants=mdi.findAllMerchants();
		merchants.forEach(m->System.out.println(m.getAccount()));
		Assert.assertNotNull(merchants);
	}
	@Test
	public void testPagination(){
		BaseConditionQuery<Merchant> baseConditionQuery=new BaseConditionQuery<>();
		baseConditionQuery.setLimit(3);
		baseConditionQuery.setPage(0);
		Sort sort=new Sort(Direction.DESC,"_id");
		baseConditionQuery.setSort(sort);
		SearchFilter filter=new SearchFilter("account", SearchFilter.Operator.LIKE, "Json");
		SearchFilter[] filters=new SearchFilter[1];
		filters[0]=filter;
		baseConditionQuery.setSearchFilters(filters);
		System.out.println(JSONUtil.objetc2Json(mdi.findMerhchantWithPage(baseConditionQuery)));
	}

}
